package com.weavernorth.humanaffairs;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 终端调动流程更新店铺信息表店经理字段
 */


public class TransferUpStoreAction implements Action {
    public static Log log = LogFactory.getLog(TransferUpStoreAction.class.getName());

    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsUp = new RecordSet();
        //当前日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String strCurr = simpleDateFormat.format(new Date());
        //请求id
        String strRequestid = requestInfo.getRequestid();
        //表名
        String strTable = requestInfo.getRequestManager().getBillTableName();
        //查询当前数据
        String strSql = "select  gwmc,gwmch,ddrxm,bm,bmh from " + strTable + " where requestid=? and rzsj<?";
        log.info("调动流程操作店铺信息表sql:" + strSql + ",requestid:" + strRequestid + ",当前日期:" + strCurr);
        rs.executeQuery(strSql, strRequestid, strCurr);
        if (rs.next()) {
            //调动前岗位id
            String strBeforeJobId = Util.null2String(rs.getString("gwmc"));
            //调动前岗位名称
            String strBeforeJobname = getJobName(strBeforeJobId);
            //调动后岗位id
            String strBehindJobId = Util.null2String(rs.getString("gwmch"));
            //调动后岗位名称
            String strBehindJobName = getJobName(strBehindJobId);

            //调动前部门
            String strBeforeBm = Util.null2String(rs.getString("bmh"));
            //调动后部门
            String strBehindBm = Util.null2String(rs.getString("bm"));
            //调动人id
            String strUserid = Util.null2String(rs.getString("ddrxm"));
            //获取店经理的名称
            String strMangername="";
            try{
                strMangername = new String(bb.getPropValue("mangerjobname", "mangername").getBytes("iso8859-1"), "utf-8");
                log.info("配置文件中,岗位名称:"+strMangername);
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }
            //如果调动前是店经理
            if (strBeforeJobname.equals(strMangername)) {
                String strUpsql = "update uf_dpxx set dc='' where dp=?";
                log.info("调动前:调动流程更新店铺信息:" + strUpsql + ",部门:" + strBeforeBm);
                rsUp.executeUpdate(strUpsql, strBeforeBm);
            }
            //如果调动后是店经理
            if (strBehindJobName.equals(strMangername)) {
                String strUpsql = "update uf_dpxx set dc='" + strUserid + "' where dp=?";
                log.info("调动后:调动流程更新店铺信息:" + strUpsql + ",部门:" + strBehindBm);
                rsUp.executeUpdate(strUpsql, strBehindBm);
            }


        }
        return SUCCESS;
    }

    /**
     * 根据岗位id查询岗位名称
     */
    public String getJobName(String strJobid) {
        //岗位名称
        String strJobName = "";
        //数据库操作
        RecordSet rs = new RecordSet();
        String sql = "select jobtitlename from hrmjobtitles where id=?";
        rs.executeQuery(sql, strJobid);
        if (rs.next()) {
            strJobName = Util.null2String(rs.getString("jobtitlename"));
        }
        return strJobName;

    }

}



